package com.controller;

import com.bean.User;
import com.service.UserService;
import com.util.OrderAssistant;
import com.util.PageAssistant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("login")
    public String login(HttpSession session, User user){
        System.out.println(">>> user login");
        System.out.println(user);
        System.out.println("------------------------------------------------");

        //查询
        List<User> userList = userService.find(user);

        //判断, 是否登录成功
        if (userList.size() == 1){
            session.setAttribute("loginInfo", userList.get(0));
            return "redirect:/page/main.jsp";
        }

        session.setAttribute("msg", "用户名或密码错误!");
        return "redirect:/login.jsp";
    }

    @RequestMapping("logoff")
    public String logoff(HttpSession session){
        System.out.println(">>> user logoff");
        System.out.println("------------------------------------------------");

        //销毁会话
        session.invalidate();

        return "redirect:/login.jsp";
    }

    @RequestMapping("query")
    public String query(HttpSession session, com.vo.User userQueryInfo,
                        PageAssistant<User> paUser, OrderAssistant order){
        System.out.println(">>> user query");
        System.out.println(userQueryInfo);
        System.out.println(paUser);
        System.out.println(order);
        System.out.println("------------------------------------------------");

        //创建参数集合
        Map<String, Object> map = new HashMap<>();
        //添加查询参数
        map.put("roleId", userQueryInfo.getRoleId());
        map.put("key", userQueryInfo.getRealName());

        //设置排序参数
        map.put("order", order.sql());

        //查询总记录数
        paUser.setCount(userService.count(map));

        //设置分页参数
        map.put("start", paUser.getStart());
        map.put("size", paUser.getSize());


        //查询当前页内容
        paUser.setItems(userService.query(map));

        //保存
        session.setAttribute("paUser", paUser);
        session.setAttribute("userQueryInfo", userQueryInfo.parse());
        session.setAttribute("orderInfo", order);
        loadRoleName(session);

        return "/page/user/list.jsp";
    }

    @RequestMapping("unique")
    public String unique(HttpSession session, com.vo.User user, String page){
        System.out.println(">>> user unique");
        System.out.println(user);
        System.out.println(page);
        System.out.println("------------------------------------------------");

        //查询
        List<User> userList = userService.find(user.parse());

        //保存
        session.setAttribute("userInfo", userList.get(0));

        //判断, page是否为null
//        page = page == null ? "detail.jsp" : page;
        if (page == null){
            page = "detail.jsp";
        }

        return "/page/user/" + page;
    }

    @RequestMapping("remove")
    public String remove(HttpSession session, User user){
        System.out.println(">>> user remove");
        System.out.println(user);
        System.out.println("------------------------------------------------");

        //删除
        int count = userService.remove(user);

        //保存
        String updateMsg = "删除成功!";
        String updateClass = "update-suc";
        if(count != 1){
            updateMsg = "删除失败!";
            updateClass = "update-err";
        }
        session.setAttribute("updateMsg", updateMsg);
        session.setAttribute("updateClass", updateClass);

        return "query";
    }

    @RequestMapping("modify")
    public String modify(HttpSession session, com.vo.User user){
        System.out.println(">>> user modify");
        System.out.println(user);
        System.out.println("------------------------------------------------");

        //修改
        int count = userService.modify(user.parse());

        //保存
        String updateMsg = "修改成功!";
        String updateClass = "update-suc";
        if(count != 1){
            updateMsg = "修改失败!";
            updateClass = "update-err";
        }
        session.setAttribute("updateMsg", updateMsg);
        session.setAttribute("updateClass", updateClass);

        return "/user/unique";
    }

    @RequestMapping("save")
    public String save(HttpSession session, com.vo.User user){
        System.out.println(">>> user save");
        System.out.println(user);
        System.out.println("------------------------------------------------");

        //添加
        int count = userService.save(user.parse());

        //保存
        String updateMsg = "添加成功!";
        String updateClass = "update-suc";
        if(count != 1){
            updateMsg = "添加失败!";
            updateClass = "update-err";
        }
        session.setAttribute("updateMsg", updateMsg);
        session.setAttribute("updateClass", updateClass);

        return "unique";
    }

    /**
     * 保存职务名称数组
     * @param session
     */
    private void loadRoleName(HttpSession session){

        //获取会话中的职务数组
        String[] roleList = (String[]) session.getAttribute("roleList");

        //判断, 是否为null
        if (roleList == null){

            session.setAttribute("roleList", new String[]{
                    "经理", "人事专员", "客户专员", "销售专员"
            });
        }
    }
}
